package com.example.kill.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.example.kill.dao.KillUserMapper;
import com.example.kill.exception.GlobalException;
import com.example.kill.pojo.KillUser;
import com.example.kill.redis.KillUserKey;
import com.example.kill.redis.RedisService;
import com.example.kill.result.CodeMsg;
import com.example.kill.service.KillUserService;
import com.example.kill.util.MD5Util;
import com.example.kill.util.UUIDUtil;
import com.example.kill.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Service
public class KillUserServiceImpl implements KillUserService {

    @Resource
    private KillUserMapper userMapper;


    @Autowired
    private RedisService redisService;


    public static final String COOKI_NAME_TOKEN = "token";


    @Override
    public KillUser getKillUserById(long id) {
        //取缓存
        KillUser user = redisService.get(KillUserKey.getById,""+id,KillUser.class);
        if (user!=null){
            return user;
        }
        //取数据库
        user = userMapper.selectByPrimaryKey(id);
        if (user!=null){
            redisService.set(KillUserKey.getById,""+id,user);
        }
        return user;
    }

    @Override
    public boolean login(HttpServletResponse response ,LoginVo loginVo) {
        if (loginVo == null)
        {
            throw  new GlobalException(CodeMsg.SERVER_ERROR);
        }
        String mobile = loginVo.getMobile();
        String formPass = loginVo.getPassword();

        KillUser user =  getKillUserById(Long.parseLong(mobile));

        if (user==null)
        {
            throw  new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }

        String dbPass = user.getPassword();
        String salt = user.getSalt();

        String  calcPass =  MD5Util.formPassToDBPass(formPass,salt);
        if (!calcPass.equals(dbPass))
        {
            throw  new GlobalException(CodeMsg.PASSWORD_ERROR);
        }

        //分布式session
        String token  = UUIDUtil.uuid();
        addCookie(response,token,user);

        return true;
    }

    @Override
    public KillUser getKillUserByToken(HttpServletResponse response ,String token) {
        if (StringUtils.isEmpty(token)){
            return  null;
        }
        KillUser  user =  redisService.get(KillUserKey.token,token,KillUser.class);
        //延长有效期
        if(user !=null)
        {
            addCookie(response,token,user);
        }
        return  user;
    }



    private void addCookie(HttpServletResponse response, String token, KillUser user) {

        redisService.set(KillUserKey.token, token, user);
        Cookie cookie = new Cookie(COOKI_NAME_TOKEN, token);
        cookie.setMaxAge(KillUserKey.token.expireSeconds());
        cookie.setPath("/");
        response.addCookie(cookie);
    }




}
